namespace PMS.BLL
{
    using PMS.DBUtility;
    using PMS.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class AdminBLL
    {
        public int Add(Admin model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into Admin(");
            builder.Append("LoginName,Password,RoleID,OutUseDate,IsEnable");
            builder.Append(") values (");
            builder.Append("@LoginName,@Password,@RoleID,@OutUseDate,@IsEnable");
            builder.Append(") ");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LoginName", model.LoginName), new SqlParameter("@Password", model.Password), new SqlParameter("@RoleID", model.RoleID), new SqlParameter("@OutUseDate", model.OutUseDate), new SqlParameter("@IsEnable", model.IsEnable) };
            object single = DBHelper.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int AdminID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Admin ");
            builder.Append(" where AdminID=@AdminID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AdminID", SqlDbType.Int, 4) };
            cmdParms[0].Value = AdminID;
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string AdminIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from Admin ");
            builder.Append(" where ID in (" + AdminIDlist + ")  ");
            return (DBHelper.ExecuteSql(builder.ToString()) > 0);
        }

        public List<Admin> GetList()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * ");
            builder.Append(" FROM view_Admin ");
            DataTable table = DBHelper.Query(builder.ToString()).Tables[0];
            Admin item = null;
            List<Admin> list = null;
            if (table.Rows.Count > 0)
            {
                list = new List<Admin>();
                foreach (DataRow row in table.Rows)
                {
                    item = new Admin();
                    if (row["AdminID"].ToString() != "")
                    {
                        item.AdminID = int.Parse(row["AdminID"].ToString());
                    }
                    item.LoginName = row["LoginName"].ToString();
                    item.Password = row["Password"].ToString();
                    if (row["RoleID"].ToString() != "")
                    {
                        item.RoleID = int.Parse(row["RoleID"].ToString());
                    }
                    item.RoleName = row["RoleName"].ToString();
                    if (row["OutUseDate"].ToString() != "")
                    {
                        item.OutUseDate = DateTime.Parse(row["OutUseDate"].ToString());
                    }
                    item.IsEnable = bool.Parse(row["IsEnable"].ToString());
                    list.Add(item);
                }
            }
            return list;
        }

        public Admin GetModel(int AdminID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select AdminID, LoginName, Password, RoleID, OutUseDate ,RoleName,IsEnable");
            builder.Append("  from view_Admin ");
            builder.Append(" where AdminID=@AdminID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AdminID", SqlDbType.Int, 4) };
            cmdParms[0].Value = AdminID;
            Admin admin = new Admin();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["AdminID"].ToString() != "")
                {
                    admin.AdminID = int.Parse(row["AdminID"].ToString());
                }
                admin.LoginName = row["LoginName"].ToString();
                admin.Password = row["Password"].ToString();
                if (row["RoleID"].ToString() != "")
                {
                    admin.RoleID = int.Parse(row["RoleID"].ToString());
                }
                admin.RoleName = row["RoleName"].ToString();
                if (row["OutUseDate"].ToString() != "")
                {
                    admin.OutUseDate = DateTime.Parse(row["OutUseDate"].ToString());
                }
                admin.IsEnable = bool.Parse(row["IsEnable"].ToString());
                return admin;
            }
            return null;
        }

        public Admin Login(string username, string password)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select AdminID, LoginName, Password, RoleID, OutUseDate ,RoleName,IsEnable ");
            builder.Append("  from view_Admin ");
            builder.Append("  where LoginName=@LoginName  AND Password= @Password");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@LoginName", username), new SqlParameter("@Password", password) };
            Admin admin = new Admin();
            DataSet set = DBHelper.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count > 0)
            {
                DataRow row = set.Tables[0].Rows[0];
                if (row["AdminID"].ToString() != "")
                {
                    admin.AdminID = int.Parse(row["AdminID"].ToString());
                }
                admin.LoginName = row["LoginName"].ToString();
                admin.Password = row["Password"].ToString();
                if (row["RoleID"].ToString() != "")
                {
                    admin.RoleID = int.Parse(row["RoleID"].ToString());
                }
                admin.RoleName = row["RoleName"].ToString();
                if (row["OutUseDate"].ToString() != "")
                {
                    admin.OutUseDate = DateTime.Parse(row["OutUseDate"].ToString());
                }
                admin.IsEnable = bool.Parse(row["IsEnable"].ToString());
                return admin;
            }
            return null;
        }

        public bool Update(Admin model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update Admin set ");
            builder.Append(" LoginName = @LoginName , ");
            builder.Append(" Password = @Password , ");
            builder.Append(" RoleID = @RoleID , ");
            builder.Append(" OutUseDate = @OutUseDate,  ");
            builder.Append(" IsEnable = @IsEnable  ");
            builder.Append(" where AdminID=@AdminID ");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AdminID", model.AdminID), new SqlParameter("@LoginName", model.LoginName), new SqlParameter("@Password", model.Password), new SqlParameter("@RoleID", model.RoleID), new SqlParameter("@OutUseDate", model.OutUseDate), new SqlParameter("@IsEnable", model.IsEnable) };
            return (DBHelper.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

